<template>
  <div id="testlist">
    <el-dialog
      :title="dialogtitle"
      :visible.sync="porjectEidtFormVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <projectEdit @dialogHide="saveSuccess" :projectId="projectId" :readOnly="readonly" :timestamp="timestamp" :showUnit="showUnit"></projectEdit>
    </el-dialog>
    <el-dialog
      title="预览表单"
      :visible.sync="formShowVisible"
      width="98%" top="20px" :close-on-click-modal="false" :close-on-press-escape="false">
      <formShow :projectId="projectId" :readOnly="readonly" :timestamp="timestamp"></formShow>
    </el-dialog>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item>干部监督统计</el-breadcrumb-item>
    </el-breadcrumb>
    <!--工具条-->
    <section class="wboxs">
      <el-form :inline="true" :model="filters">
        <div>
          <el-form-item label="项目名称">
            <el-input v-model="filters.key" placeholder="关键字" @keyup.enter.native="searchPrj"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchPrj" icon="el-icon-search">查询</el-button>
          </el-form-item>
          <el-form-item v-if="buttonSave">
            <el-button type="success" @click="addnew" icon="el-icon-plus">新建</el-button>
          </el-form-item>
          <el-form-item v-if="buttonAuth">
            <el-button type="warning" @click="handleAuth" icon="el-icon-user" style="color: #fff !important;">授权</el-button>
          </el-form-item>
        </div>
      </el-form>
    </section>
    <!--列表-->
    <section class="wboxd" style="margin-top:10px">
      <h1>项目列表</h1>
      <el-table :data="prjs" highlight-current-row v-loading="listLoading" style="width: 100%;" @sort-change='sortChange' :height="tableHeight" border>
        <el-table-column type="index" label=" " width="50" align="center" class-name="tableth">
        </el-table-column>
        <el-table-column prop="projectTitle" label="项目名称" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="类型" width="120" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ fTypeName(scope.row.projectType) }}
          </template>
        </el-table-column>
        <el-table-column prop="createUsername" label="创建人" width="100" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="创建时间" width="170" prop="dataCreateDatetime">
          <template slot-scope="scope">
            {{dateFtt("yyyy-MM-dd hh:mm:ss",scope.row.dataCreateDatetime)}}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center" v-if="buttonSave">
          <template slot-scope="scope">
            <template  v-if="buttonAuth||scope.row.mine">
              <el-tag v-if="scope.row.projectState===1" type="success" effect="dark" style="cursor:pointer" title="点击停用此项目" @click="setPrj(scope.row.id,2)">启用</el-tag>
              <el-tag v-else type="info" effect="dark" style="cursor:pointer" title="点击启用此项目" @click="setPrj(scope.row.id,1)">停用</el-tag>
            </template>
            <template v-else>
              <el-tag v-if="scope.row.projectState===1" type="success" effect="dark">启用</el-tag>
              <el-tag v-else type="info" effect="dark">停用</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="开始时间" width="100">
          <template slot-scope="scope">
            {{dateFtt("yyyy-MM-dd",scope.row.beginDate)}}
          </template>
        </el-table-column>
        <el-table-column label="结束时间" width="100">
          <template slot-scope="scope">
            {{dateFtt("yyyy-MM-dd",scope.row.endDate)}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="500">
          <template slot-scope="scope">
            <el-button size="small" @click="editPrj(scope.row.id)" type="text" icon="el-icon-edit" v-if="buttonAuth||scope.row.mine">编辑项目</el-button>
            <el-button size="small" @click="unitList(scope.row.id)" type="text" icon="el-icon-s-order" v-if="scope.row.projectState!==0">填报情况</el-button>
            <el-button size="small" @click="delPrj(scope.row.id)" type="text" icon="el-icon-delete" v-if="buttonAuth||scope.row.mine">删除项目</el-button>
          </template>
        </el-table-column>
      </el-table>
    <div class="block">
      <el-pagination :total="totalnum" :page-size="pageSize" @current-change="handleCurrentChange" :current-page="pageNum" @size-change="handleSizeChange" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </div>
    </section>
  </div>
</template>

<script>
import getunits from '@/js/common.js'
import projectEdit from './detail'
import formShow from './formShow'
export default {
  components: {
    projectEdit, formShow
  },
  data () {
    return {
      formShowVisible: false,
      showUnit: false,
      readonly: true,
      prjs: [],
      projectId: null,
      dialogtitle: '新建项目',
      timestamp: '',
      porjectEidtFormVisible: false,
      porjectEidtFormLoading: false,
      porjectEidtForm: {
        beginDate: '',
        endDate: '',
        fileId: '',
        projectId: '',
        title: '',
        unitIds: []
      },
      filters: {
        key: ''
      },
      total: 0,
      pageNum: 1,
      pageSize: 20,
      listLoading: false,
      totalnum: 0,
      Isace: true,
      Sortkey: '',
      changeSizeFlag: false,
      tableHeight: 100,
      statusdic: [],
      myloading: false,
      myname: '',
      typeNameList: ['因私出国（境）'],
      buttonAuth: false,
      buttonSave: false
    }
  },
  methods: {
    fTypeName (index) {
      return this.typeNameList[index]
    },
    handleAuth () {
      this.$router.push({name: 'supervisionAuth'})
    },
    inRoles (str) {
      let arr = JSON.parse(localStorage.getItem('Roles'))
      // arr = 'aaabbROLE_admin'
      if (arr.indexOf(str) === -1) {
        return false
      } else {
        return true
      }
    },
    unitList (projectId) {
      this.$router.push('/supervision/unitList/' + projectId)
    },
    saveSuccess () {
      this.porjectEidtFormVisible = false
      this.getPrj()
    },
    addnew () {
      this.projectId = 0
      this.readonly = false
      this.timestamp = new Date().getTime()
      this.porjectEidtFormVisible = true
    },
    detailPrj (id) {
      this.dialogtitle = '项目详情'
      this.projectId = id
      this.readonly = true
      this.timestamp = new Date().getTime()
      this.porjectEidtFormVisible = true
    },
    showForm (id) {
      this.preview = true
      this.projectId = id
      this.readonly = false
      this.timestamp = new Date().getTime()
      this.formShowVisible = true
    },
    editPrj (id) {
      this.dialogtitle = '编辑项目'
      this.projectId = id
      this.readonly = false
      this.timestamp = new Date().getTime()
      this.porjectEidtFormVisible = true
    },
    setPrj (id, flag) {
      let flagtext = '启用'
      if (flag === 2) {
        flagtext = '停用'
      }
      this.$confirm('确定' + flagtext + '该项目填报?', '提示', {
        type: 'success'
      }).then(() => {
        this.myloading = true
        let para = {
          projectId: id,
          state: flag
        }
        this.$http.post(this.$http.defaults.supervisionAPI + `api/statsProjectData/updateProjectState`, para).then(response => {
          if (response.data.success === true) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
          } else {
            console.log('操作失败:' + response.data.message)
            this.$message({
              message: '操作失败:' + response.data.message,
              type: 'error'
            })
          }
          this.myloading = false
          this.getPrj()
        })
      })
    },
    handleSizeChange (val) {
      this.changeSizeFlag = true
      this.pageNum = 1
      this.pageSize = val
      this.getPrj()
    },
    handleCurrentChange (val) {
      if (this.changeSizeFlag === false) {
        this.pageNum = val
        this.getPrj()
      }
    },
    querySearch (queryString, cb) {
      return getunits(this, queryString, cb)
    },
    // 查询
    searchPrj () {
      this.pageNum = 1
      this.getPrj()
    },
    // 排序
    sortChange: function (column, prop, order) {
      this.Sortkey = column.prop
      if (column.order === 'ascending') {
        this.Isace = true
      } else {
        this.Isace = false
      }
      this.getPrj()
    },
    getPrj () {
      let para = {
        keyWords: this.filters.key,
        orderField: this.Sortkey,
        orderType: this.Isace,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      this.listLoading = true
      // var personParams = { Page: 0, Pagenum: 20, Personname: '', Unitkey: '', Sortkey: '', Isace: true, timestamp: new Date().getTime() }
      this.$http
        .post(this.$http.defaults.supervisionAPI + `api/statsProjectData/queryByPage`, para)
        .then(response => {
          this.listLoading = false
          this.prjs = response.data.result.records
          this.totalnum = response.data.result.total
          this.changeSizeFlag = false
        })
        .catch(error => {
          this.$message({
            message: '获取失败:' + error,
            type: 'error'
          })
        })
    },
    delPrj (id) {
      var _this = this
      this.$confirm('确认删除该项目吗?', '提示', {
        type: 'error'
      }).then(() => {
        this.$http.post(this.$http.defaults.supervisionAPI + `api/statsProjectData/remove`, {ids: [id]}).then(function (response) {
          if (response.data.success === true) {
            _this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            console.log('删除失败:' + response.data.message)
            _this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          _this.getPrj()
        })
      }).catch(() => {
      })
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - 292
      })
    },
    // getDics () {
    //   var _this = this
    //   this.$http
    //     .get(`/api/prj/dic`)
    //     .then(function (response) {
    //       _this.statusdic = response.data.statusdic
    //       _this.yearlist = response.data.yeardic
    //     })
    //     .catch(function (error) {
    //       console.log(error)
    //       _this.$message({
    //         message: '网络获取失败:' + error,
    //         type: 'error'
    //       })
    //     })
    // },
    stateText (id) {
      switch (id) {
        case 0:
          return '未启动'
        case 1:
          return '已启动'
        case 2:
          return '<i class="el-icon-success" style="color:#67C23A"></i> 已结束'
      }
    },
    getButtonAuth () {
      this.$http
        .get(this.$http.defaults.supervisionAPI + `api/statsProjectData/getButtonAuth`)
        .then(response => {
          if (response.data.success) {
            this.buttonAuth = response.data.result[0] // 授权按钮权限
            this.buttonSave = response.data.result[1] // 新增、编辑、删除、修改状态按钮权限
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          if (error.status === 500) {
            this.$message({
              message: '网络获取失败:' + error,
              type: 'error'
            })
          }
        })
    }
  },
  mounted () {
    this.myname = localStorage.getItem('userTrueName')
    this.getPrj()
    this.getButtonAuth()
    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  }
}
</script>
<style>
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
  }
  .wboxd .el-table td, .el-table th{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体'
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #testlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  .el-table--border th, .el-table--border td {
    border-right: 1px solid #95B8E7;
  }
  .el-table th.is-leaf, .el-table td {
    border-bottom: 1px solid #95B8E7;
  }

</style>
<style scoped>
  h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  .wboxs{
    padding:5px 15px;
    width: calc(100% - 30px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
